package com.ppt.android.utils

import android.content.Context
import android.os.Bundle
import android.util.Log
import com.adjust.sdk.Adjust
import com.adjust.sdk.AdjustAdRevenue
import com.adjust.sdk.AdjustEvent
import com.google.android.gms.ads.AdValue
import com.google.firebase.analytics.FirebaseAnalytics
import com.ppt.android.base.BaseApplication.Companion.context


class AnalyticsUtil private constructor() {

    companion object {
        //首页点击
        const val HOME_EVENT_CLICK = "home_event_click"
        //壁纸点击
        const val WALLPAPER_EVENT_CLICK = "wallpaper_event_click"
        //ai生成点击
        const val AI_EVENT_CLICK = "ai_event_click"
        const val RING_EVENT_CLICK = "ring_event_click"

        private var count:Int = 0
        private lateinit var firebaseAnalytics: FirebaseAnalytics

        // 初始化工具类
        fun init(context: Context) {
            firebaseAnalytics = FirebaseAnalytics.getInstance(context)
        }

        fun clickTranslateCount(){
            val adjustEvent = AdjustEvent("p33rkr")
            Adjust.trackEvent(adjustEvent)
            count++
            Log.d("ad_test", "翻译次数---${count}")
            if (count % 3 == 0) {
                clickThreeTranslateCount()
            }
            if (count % 5 == 0) {
                clickFiveTranslateCount()
            }
        }

        private fun clickThreeTranslateCount(){
            val adjustEvent = AdjustEvent("2e0anb")
            Adjust.trackEvent(adjustEvent)
        }

        private fun clickFiveTranslateCount(){
            val adjustEvent = AdjustEvent("drincq")
            Adjust.trackEvent(adjustEvent)
        }

        fun sendRevenueToAdjust(adValue: AdValue, adType: String, adUnitId: String) {
            // 1. 创建 Adjust 广告收入对象
            val adjustAdRevenue = AdjustAdRevenue("AdMob")

            // 2. 设置收益数据（转换为美元）
            val revenue = adValue.valueMicros / 1_000_000.0  // 微美元 → 美元
            adjustAdRevenue.setRevenue(revenue, adValue.currencyCode)

            // 3. 添加元数据（可选但推荐）
            adjustAdRevenue.addCallbackParameter("ad_unit", adUnitId)
            adjustAdRevenue.addCallbackParameter("ad_format", adType)
            adjustAdRevenue.addCallbackParameter("network", "AdMob")

            // 4. 发送给 Adjust
            Adjust.trackAdRevenue(adjustAdRevenue)

            val bundle = Bundle().apply {
                putString(FirebaseAnalytics.Param.AD_PLATFORM, "tradplus")
                putString(FirebaseAnalytics.Param.AD_SOURCE, "AdMob")
                putString(FirebaseAnalytics.Param.AD_FORMAT, adType)
                putString(FirebaseAnalytics.Param.AD_UNIT_NAME, adUnitId)
                putDouble(FirebaseAnalytics.Param.VALUE, revenue)
                putString(FirebaseAnalytics.Param.CURRENCY, "USD")
            }
            //firebase 收益
            FirebaseAnalytics.getInstance(context).logEvent(FirebaseAnalytics.Event.AD_IMPRESSION, bundle)
        }


        // 记录自定义事件
        fun logEvent(eventName: String, params: Bundle? = null) {
            firebaseAnalytics.logEvent(eventName, params)
        }

        // 设置用户属性
        fun setUserProperty(name: String, value: String) {
            firebaseAnalytics.setUserProperty(name, value)
        }

        // 记录屏幕浏览事件
        fun logScreenView(screenName: String, screenClass: String? = null) {
            val params = Bundle().apply {
                putString(FirebaseAnalytics.Param.SCREEN_NAME, screenName)
                screenClass?.let { putString(FirebaseAnalytics.Param.SCREEN_CLASS, it) }
            }
            firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, params)
        }

        // 记录广告展示事件
        fun logAdImpression(adUnitId: String, adType: String) {
            val params = Bundle().apply {
                putString("ad_unit_id", adUnitId)
                putString("ad_type", adType)
            }
            firebaseAnalytics.logEvent("ad_impression", params)
        }

        // 记录广告点击事件
        fun logAdClick(adUnitId: String, adType: String) {
            val params = Bundle().apply {
                putString("ad_unit_id", adUnitId)
                putString("ad_type", adType)
            }
            firebaseAnalytics.logEvent("ad_click", params)
        }

        // 设置用户 ID
        fun setUserId(userId: String) {
            firebaseAnalytics.setUserId(userId)
        }

        // 启用/禁用数据收集
        fun setAnalyticsEnabled(enabled: Boolean) {
            firebaseAnalytics.setAnalyticsCollectionEnabled(enabled)
        }
    }
}
